Dynamic_cast` from Base to Derived?

In C++, each instance of a class has its own version of datatypes, but all classes share the same function in memory (other than for inline functions). In your case, when you say something like.

Up vote 5 down vote favorite 1 share g+ share fb share tw.

Yes, I know that downcast using dynamic_cast can't compile if the Base is not polymorphic, but my problem is not about this. Class Base { public: virtual void bar() { cout (pb); pd->foo(); //outputs foo pb = new Base; //Base* points to a Base object pd = dynamic_cast(pb); pd->foo(); //outputs foo, too. Why?

} I thought when pb = new Derived;, pb actually points to a Derived object lies in heap. After pd = dynamic_cast(pb);, pd also points to that Derived object, so pd->foo() should be OK. But when pb = new Base;, what pb points to is a Base object in heap, then after pd = dynamic_cast(pb);, how could pd->foo() works?

Did dynamic_cast turn the Base object in heap into a Derived object? C++ dynamic-cast link|improve this question asked Apr 2 at 9:15Alcott1,452212 89% accept rate.

In C++, each instance of a class has its own version of datatypes, but all classes share the same function in memory (other than for inline functions). In your case, when you say something like: pd->foo(); You are essentially calling Derived::foo, which is a function in memory and the compiler knows where it is. The thing is, it is not dependent on pd at all.

However, if you had something like this: class Derived : public Base { private: int a; public: Derived() { a = 100; } void foo() { std::coutfoo() will cause a Segmentation fault. Here, your dynamic cast has failed and when Derived::foo is called, it is passed 0 as the this object. It was fine in the previous case, as the this object was never used.

However, in the second case, it is used and hence, causes a Segmentation fault.

– Alcott Apr 2 at 9:25 @Alcott yes, but the this parameter will be passed as NULL (as that is the value of pd). Hence the expected crash when you dereferencing it by accessing a in Rohan's example. – littleadv Apr 2 at 9:26 1 It is largely compiler dependent, and as @Luchian Grigore has mentioned, anything can happen.

So, in most cases, yes, but it is something you cannot count on. – Rohan Prabhu Apr 2 at 9:26.

In your foo you don't access this, which in this case should be NULL. That is what dynamic_cast returns when the cast cannot be done. Basically you're in the "undefined behavior" area here.

FYI, I +1'd your answer because it's correct. But still don't see how something like this is lucky. You can run into nasty bugs which are difficult to track because the program didn't crash when it should have.

– Luchian Grigore Apr 2 at 9:22 1 @LuchianGrigore Luck (n) 1. Something that happens to someone by chance, a chance occurrence. – Potatoswatter Apr 2 at 9:24.

You're running into undefined behavior. You should check the return type of dynamic_cast. Pd = dynamic_cast(pb); This returns null, and you call a function on a NULL pointer.

Anything can happen.

– Alcott Apr 2 at 9:22 @Alcott it's undefined behavior. Anything can happen. – Luchian Grigore Apr 2 at 9:22 1 @Alcott technically Luchian's answer is correct.

But practically the reason is because its simpler for the compiler developers to just ignore this, as they're allowed (because the standard doesn't define what to do in such case), instead of implementing complicated and performance-costing run time checks on every pointer access. Thus whatever happens - happens, and anything is correct. – littleadv Apr 2 at 9:24 @littleadv, alright, that'll be dangerous, will it?

– Alcott Apr 2 at 9:27 @Alcott all undefined behavior is. Just don't do it. – Luchian Grigore Apr 2 at 9:28.

Please always prefer to check whether the cast is successful before trying to use it. I guess its the advantage of using casting. You can check whether its successful or not.

Pd = dynamic_cast(pb); if(pd! =NULL) pd->foo(); If the cast fails pd has value NULL. Do not use pd unless you are sure it has a value.

And then only de reference it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions